<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>数据</el-breadcrumb-item>
        <el-breadcrumb-item>书籍</el-breadcrumb-item>
        <el-breadcrumb-item>章节次日留存</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
      >
        <!-- 自定义按钮 -->
        <template slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-download"
            size="small"
            plain
            @click="handleExcel()"
            >导出Excel</el-button
          >
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { handleExcelCat } from "@/api/exportCommon";
import { appStatisticsChapterNextdayBackread as baseUrl } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
        // pageSizes:[1,2,3]
      },
      type: true,
      data: [],
      params: {},
      form: {},
      option: {
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        excelBtn: false,
        selection: true,
        border: true,
        menu: false,
        index: true,
        indexLabel: "序号",
        column: [
          {
            label: "日期",
            prop: "stats_date",
            type: "input",
          },
          {
            label: "章节id",
            type: "input",
            prop: "chapter_id",
            search: true,
          },
          {
            label: "章节名称",
            prop: "chapter_name",
            type: "input",
            search: true,
          },
          {
            label: "书籍id",
            prop: "book_id",
            type: "input",
            search: true,
          },
          {
            label: "书籍名称",
            prop: "book_name",
            type: "input",
            search: true,
          },
          {
            label: "当日完读人数",
            prop: "today_over_read_uv",
            type: "input",
          },
          {
            label: "次日回读人数",
            prop: "nextday_read_uv",
            type: "input",
          },
          {
            label: "当日阅读章节总人数",
            prop: "today_read_uv",
            type: "input",
          },
          {
            label: "章节留存率",
            prop: "keep_scale",
            type: "input",
          },
          {
            label: "日期",
            type: "daterange",
            prop: "daterange",
            hide: true,
            search: true,
          },
        ],
      },
    };
  },
  methods: {
    //数据导出
    async handleExcel() {
      this.loading = true;
      //处理搜索条件
      let params = {...this.$refs.crud.searchForm};
      params.orderByList = "stats_date&DESC";

      //日期
      if (params.daterange && params.daterange.length > 0) {
        params.stats_date = `!<>!${params.daterange}`;
      }
      delete params.daterange;
      //名称模糊搜索
      if (params.book_name) {
        params.book_name = `%${params.book_name}%`;
      }
      if (params.chapter_name) {
        params.chapter_name = `%${params.chapter_name}%`;
      }

      //调用导出接口
      handleExcelCat(
        this.$export,
        baseUrl.pageAndParam,
        { ...params, ...this.default },
        "章节次日留存",
        this.page.total,
        [...this.option.column]
      );
      this.loading = false;
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      this.params.orderByList = "stats_date&DESC";
      //发送请求获取数据
      let response = await common(
        `${baseUrl.pageAndParam}`,
        { ...this.params, ...this.defalut },
        this.page,
        "POST"
      );

      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      //日期
      if (params.daterange) {
        params.stats_date = `!<>!${params.daterange}`;
      }
      //名称模糊搜索
      if (params.book_name) {
        params.book_name = `%${params.book_name}%`;
      }
      if (params.chapter_name) {
        params.chapter_name = `%${params.chapter_name}%`;
      }
      this.params = params;
      this.page.currentPage = 1;
      delete params.daterange;
      this.getList();
      if (done) {
        done();
      }
    },
  },
};
</script>